package com.txsj.scan.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.greendao.GoodsInfoDao;
import com.txsj.scan.R;
import com.txsj.scan.activity.LabelActivity;
import com.txsj.scan.adapter.GoodsLeftAdapeter;
import com.txsj.scan.adapter.SimpleGoodsInfoAdapter;
import com.txsj.scan.base.BaseFragment;
import com.txsj.scan.bean.GoodsClassify;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.db.GreenDaoUtils;
import com.txsj.scan.presenter.SimpleGoodsListPresenter;
import com.txsj.scan.refresh.XRefreshView;
import com.txsj.scan.view.ISimpleGoodsListView;

import java.util.List;

/**
 * @Created by WangChuangJun on 2019/2/25
 * @Describe
 */
public class AllGoodsFragment extends BaseFragment<SimpleGoodsListPresenter> implements ISimpleGoodsListView {

    public static final int REQUEST_SEARCH_CODE = 0x021;

    private RecyclerView type_recyclerView, goods_recyclerView;
    private XRefreshView xRefreshView;
    private GoodsLeftAdapeter leftAdapeter;
    private SimpleGoodsInfoAdapter goodsInfoAdapter;
    private LinearLayout left_layout;
    private ImageView iv_search_goods;

    int pageNo = 1;
    int classifyPosition = 0;

    boolean isGoodsTypeLoading = false;

    @Override
    protected int layoutId() {
        return R.layout.fragment_all_goods;
    }

    @Override
    public void init() {
        initView();
        initData();
    }

    private void initData() {
        //初始化商品分类列表
        presenter.getGoodsTypeHttp();
    }

    private void initView() {

        iv_search_goods = (ImageView) rootView.findViewById(R.id.iv_search_goods);

        type_recyclerView = (RecyclerView) rootView.findViewById(R.id.type_recyclerView);
        goods_recyclerView = (RecyclerView) rootView.findViewById(R.id.goods_recyclerView);
        xRefreshView = (XRefreshView) rootView.findViewById(R.id.xRefreshView);

        initEmptyView();
        xRefreshView.enableEmptyView(false);
        xRefreshView.setPullRefreshEnable(true);
        xRefreshView.setAutoLoadMore(true);

        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                pageNo = 1;
                if (leftAdapeter.getList().size() > 0) {
                    presenter.getGoodsList(leftAdapeter.getList().get(classifyPosition).getGoods_class_id(), pageNo, "-1", "0");
                }
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                if (goodsInfoAdapter.getList().size() % 20 != 0 || leftAdapeter.getList().size() <= 0) {
                    xRefreshView.stopLoadMore();
                    return;
                }
                presenter.getGoodsList(leftAdapeter.getList().get(classifyPosition).getGoods_class_id(), pageNo, "-1", "0");
            }
        });

        type_recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        leftAdapeter = new GoodsLeftAdapeter(activity);
        type_recyclerView.setAdapter(leftAdapeter);
        leftAdapeter.setOnItemListener(new GoodsLeftAdapeter.OnItemListener() {
            @Override
            public void onClick(View v, int pos) {
                classifyPosition = pos;
                pageNo = 1;
                leftAdapeter.setSelect(pos);
                goodsInfoAdapter.clear();
                xRefreshView.startRefresh();
            }
        });

        goods_recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        goodsInfoAdapter = new SimpleGoodsInfoAdapter(activity);
        goods_recyclerView.setAdapter(goodsInfoAdapter);
        goodsInfoAdapter.setOnSecondItemClickListener(new SimpleGoodsInfoAdapter.ISetOnNewSecondClick() {

            @Override
            public void onClick(int position) {
                GoodsInfo goodsInfo = goodsInfoAdapter.get(position);
                Intent intent = activity.getIntent();
                intent.putExtra("data", new Gson().toJson(goodsInfo));
                activity.setResult(LabelActivity.RESULT_GOODS_CODE,intent);
                activity.finish();

                List<GoodsInfo> list = GreenDaoUtils.getSingleTon().getmDaoSession()
                        .getGoodsInfoDao().queryBuilder()
                        .where(GoodsInfoDao.Properties.Goods_id.eq(goodsInfo.getGoods_id()))
                        .list();
                GoodsInfo info = null;
                if (null != list && list.size() > 0&& null != list.get(0)) {
                    info = list.get(0);
                    info.setBind_memory(1);
                    GreenDaoUtils.getSingleTon().getmDaoSession().getGoodsInfoDao().insertOrReplaceInTx(info);
                }
                if (info == null){
                    goodsInfo.setBind_memory(1);
                    GreenDaoUtils.getSingleTon().getmDaoSession().getGoodsInfoDao().insertOrReplaceInTx(goodsInfo);
                }

            }
        });


    }

    @Override
    protected SimpleGoodsListPresenter providePresenter() {
        return new SimpleGoodsListPresenter(this);
    }

    @Override
    public Context context() {
        return activity;
    }

    @Override
    public void onError(String msg) {
        hideLoading();
        t(msg);
        isGoodsTypeLoading = false;
    }

    @Override
    public void onLoadMoreError(String msg) {
        //加载更多失败
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(false);
    }

    @Override
    public void onLoadError(String msg) {
        //商品第一次加载失败
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(true);
    }

    @Override
    public void onLoadMoreEnd(String msg) {
        //商品不是第一次，加载结束
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(false);
    }

    @Override
    public void onEmpty(String msg) {
        //商品第一次加载为空
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(true);
    }


    @Override
    public void onLoading() {
        showLoading();
    }

    @Override
    public void onClassifySuccess(List<GoodsClassify> data) {
        hideLoading();
        isGoodsTypeLoading = false;
        leftAdapeter.setData(data);
        classifyPosition = 0;
        leftAdapeter.setSelect(classifyPosition);
        xRefreshView.startRefresh();
    }

    @Override
    public void onGoodsInfoListSuccess(int count,List<GoodsInfo> data) {
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();

        xRefreshView.enableEmptyView(false);
        goodsInfoAdapter.setRightListData(count,data, pageNo, leftAdapeter.getList().get
                (classifyPosition)
                .getClass_name());
        //在加载成功后进行pageNo++
        pageNo++;
    }

    Button btn_empty_reload;

    /**
     * 列表数据为空时 显示的View
     */
    private void initEmptyView() {
        View emptyView = View.inflate(activity, R.layout.view_empty, null);
        emptyView.setBackgroundColor(getResources().getColor(R.color.white));
        btn_empty_reload = (Button) emptyView.findViewById(R.id.btn_empty_reload);
        xRefreshView.setEmptyView(emptyView);
        btn_empty_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xRefreshView.startRefresh();
            }
        });

    }
}
